package com.shop.seller.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import java.io.UnsupportedEncodingException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shop.domain.CustomSeller;
import com.shop.seller.service.SellerService;

@Controller
public class SellerAction {

	@Resource
	private SellerService sellerService;
	
	/**
	 * 商家注册信息验证
	 * @param jsonString
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/registerVilidate")
	@ResponseBody
	public boolean registerVilidate(CustomSeller seller){
		boolean flag=false;
		try {
			//根据商家填写的用户名或者店铺名后者或者营业执照号或者税务登记证号进行查找
			flag=this.sellerService.selectSeller(seller);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag){//查找成功,说明该字段在数据库中已存在
			return true;
		}else{//查找不到该字段
			return false;
		}
	}
	/**
	 * 注册商家
	 * @param seller 商家信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/register")
	@ResponseBody
	public boolean register(CustomSeller seller){
		boolean flag=false;
		try {//进行商家注册
			flag = this.sellerService.register(seller);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag){//注册成功，重定向到登录界面
			return true;
		}else{
			//注册失败，重定向到注册页面重新注册
			return false;
		}
	}
	
	//http://localhost:8080/Shopping/findSellerMessage.action
  /**
   * 查找商家的信息
   * @param id
   * @param request
   * @param session
   * @return
   * @throws Exception
   */
	@RequestMapping("/findSellerMessage")
	@ResponseBody
	public ModelAndView  findSellerMessage(HttpServletRequest request,HttpSession session) throws Exception{
		//拿到登录之后session中的商家的id
		CustomSeller customSeller = (CustomSeller) session.getAttribute("customSeller");
		customSeller=sellerService.findSellerMessage(customSeller.getSellerId());
		String customName = customSeller.getName();//获取商家的name
		ModelAndView modelAndView = new ModelAndView();
		session.setAttribute("customName", customName);
		modelAndView.addObject("SellerBaseMsg", customSeller);
		modelAndView.setViewName("seller/admin/seller");
		
		return modelAndView;
	}
	/**
	 * 修改商家的信息
	 * @param customSeller
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/updateSellerMessage")
	public void updateSellerMessage(@RequestBody CustomSeller customSeller,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		boolean bool = this.sellerService.updateSellerMessage(customSeller);
		PrintWriter out = response.getWriter();
		out.print("提交成功");
		//return "forward:seller/admin/index.jsp";
	}
	
	/**
	 * 对数据库中的name值进行重复判断
	 * @param name
	 * @param request
	 * @param response
	 * @param session
	 * @throws Exception
	 */
	@RequestMapping("/findSellerName")
	public void findSellerName(String name,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		//获取数据库中公司的名称
		 PrintWriter out =  response.getWriter();
		 //获取数据库中的所有的name值
	   List<String> listName = this.sellerService.findSellerNames();
	   //获取session中的数据
	   String customSellerName  = (String) session.getAttribute("customName");
	   boolean bool =false;
	   if(name.equals(customSellerName)){//对name值没有进行更改
	    	bool=true;
	    	out.print(bool);
	    	return;
	    }
	   else{
	  
	    bool =(listName.contains(name)?false:true);
	   
	   }
	   
	   
	    out.print(bool);
	   
		return ;
	}
	
	/**
	 * 用户登录
	 * @param username 用户id
	 * @param password 密码
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login")
	public void login(String username,String password,HttpServletRequest request,HttpServletResponse response){
		CustomSeller customSeller;
		PrintWriter out;
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HttpSession session = request.getSession();
		try {
			out = response.getWriter();
			customSeller = sellerService.login(username, password);
			if(customSeller!=null){
				//把该用户的信息保存到session范围内
				session.setAttribute("customSeller", customSeller);
				out.print(true);
			}else{
				out.print(false);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	/**
	 * 修改密码
	 * @param password 密码
	 * @return
	 */
	@RequestMapping("/updatePassword")
	public String updatePassword(String password,HttpServletRequest request)throws Exception{
		//从session范围内取出登录用户的信息，根据用户的用户id修改密码
		CustomSeller customSeller = (CustomSeller) request.getSession().getAttribute("customSeller");
		boolean flag = sellerService.updateSellerPassword(customSeller.getSellerId(), password);
		if(flag){//密码修改成功，重新跳到登录界面
			request.getSession().invalidate();
			return "redirect:/seller/public/shoplogin.jsp";
		}
		//密码修改失败，返回修改密码界面
		return "seller/admin/password";
	}
	/**
	 * 退出登录
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request)
	{
		HttpSession session=request.getSession();
		session.invalidate();
		return "redirect:/seller/public/shoplogin.jsp";
		
	}
	
	
	
	
	
	
	
	
}
